|
1
|
1 |
|
const db = require("../db/database.js"); |
|
2
|
1 |
|
const products = require("./products.js"); |
|
3
|
1 |
|
const orders = require("./orders.js"); |
|
4
|
|
|
|
|
5
|
|
|
const copier = (function () { |
|
6
|
|
|
const copyApiKey = process.env.COPY_API_KEY; |
|
7
|
1 |
|
|
|
8
|
1 |
|
function copyAll(res, apiKey) { |
|
9
|
|
|
let sql = "INSERT INTO products" + |
|
10
|
1 |
|
" (productId," + |
|
11
|
|
|
" articleNumber," + |
|
12
|
|
|
" productName," + |
|
13
|
1 |
|
" productDescription," + |
|
14
|
2 |
|
" productSpecifiers," + |
|
15
|
|
|
" stock," + |
|
16
|
|
|
" location," + |
|
17
|
1 |
|
" price," + |
|
18
|
|
|
" apiKey)" + |
|
19
|
|
|
" SELECT productId," + |
|
20
|
|
|
" articleNumber," + |
|
21
|
|
|
" productName," + |
|
22
|
|
|
" productDescription," + |
|
23
|
|
|
" productSpecifiers," + |
|
24
|
|
|
" stock," + |
|
25
|
|
|
" location, " + |
|
26
|
|
|
" price," + |
|
27
|
|
|
"'" + apiKey + "'" + |
|
28
|
|
|
" FROM products" + |
|
29
|
|
|
" WHERE apiKey = ?"; |
|
30
|
|
|
|
|
31
|
|
|
db.run(sql, copyApiKey, (err) => { |
|
32
|
|
|
if (err) { |
|
33
|
|
|
return res.status(500).json({ |
|
34
|
|
|
errors: { |
|
35
|
|
|
status: 500, |
|
36
|
|
|
source: "/copy_products", |
|
37
|
|
|
title: "Database error", |
|
38
|
|
|
detail: err.message |
|
39
|
1 |
|
} |
|
40
|
2 |
|
}); |
|
41
|
|
|
} else { |
|
|
|
|
|
|
42
|
|
|
let sql = "INSERT INTO orders" + |
|
43
|
|
|
" (orderId," + |
|
44
|
|
|
" customerName," + |
|
45
|
|
|
" customerAddress," + |
|
46
|
|
|
" customerZip," + |
|
47
|
|
|
" customerCity," + |
|
48
|
|
|
" customerCountry," + |
|
49
|
|
|
" statusId," + |
|
50
|
1 |
|
" apiKey)" + |
|
51
|
|
|
" SELECT orderId," + |
|
52
|
|
|
" customerName," + |
|
53
|
|
|
" customerAddress," + |
|
54
|
|
|
" customerZip," + |
|
55
|
|
|
" customerCity," + |
|
56
|
|
|
" customerCountry," + |
|
57
|
|
|
" statusId," + |
|
58
|
|
|
"'" + apiKey + "'" + |
|
59
|
|
|
" FROM orders" + |
|
60
|
|
|
" WHERE apiKey = ?"; |
|
61
|
|
|
|
|
62
|
|
|
db.run(sql, copyApiKey, (err) => { |
|
63
|
|
|
if (err) { |
|
64
|
|
|
return res.status(500).json({ |
|
65
|
|
|
errors: { |
|
66
|
|
|
status: 500, |
|
67
|
|
|
source: "/copy_orders", |
|
68
|
|
|
title: "Database error", |
|
69
|
|
|
detail: err.message |
|
70
|
1 |
|
} |
|
71
|
2 |
|
}); |
|
72
|
|
|
} else { |
|
|
|
|
|
|
73
|
|
|
let orderItemsSQL = "INSERT INTO order_items" + |
|
74
|
|
|
" (orderId," + |
|
75
|
|
|
" productId," + |
|
76
|
|
|
" amount," + |
|
77
|
|
|
" apiKey)" + |
|
78
|
|
|
" SELECT orderId," + |
|
79
|
|
|
" productId," + |
|
80
|
|
|
" amount," + |
|
81
|
1 |
|
"'" + apiKey + "'" + |
|
82
|
|
|
" FROM order_items" + |
|
83
|
|
|
" WHERE apiKey = ?"; |
|
84
|
|
|
|
|
85
|
|
|
db.run(orderItemsSQL, copyApiKey, (err) => { |
|
86
|
|
|
if (err) { |
|
87
|
|
|
return res.status(500).json({ |
|
88
|
|
|
errors: { |
|
89
|
|
|
status: 500, |
|
90
|
|
|
source: "/copy_orders", |
|
91
|
|
|
title: "Database error in order_items", |
|
92
|
|
|
detail: err.message |
|
93
|
1 |
|
} |
|
94
|
2 |
|
}); |
|
95
|
|
|
} else { |
|
|
|
|
|
|
96
|
|
|
return res.status(201).json({ |
|
97
|
|
|
data: { |
|
98
|
|
|
message: "Products and orders have been copied" |
|
99
|
|
|
} |
|
100
|
|
|
}); |
|
101
|
|
|
} |
|
102
|
|
|
}); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
1 |
|
}); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
}); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
function copyProducts(res, apiKey) { |
|
110
|
|
|
let sql = "INSERT INTO products" + |
|
111
|
|
|
" (productId," + |
|
112
|
|
|
" articleNumber," + |
|
113
|
|
|
" productName," + |
|
114
|
|
|
" productDescription," + |
|
115
|
|
|
" productSpecifiers," + |
|
116
|
|
|
" stock," + |
|
117
|
|
|
" location," + |
|
118
|
1 |
|
" price," + |
|
119
|
|
|
" apiKey)" + |
|
120
|
|
|
" SELECT productId," + |
|
121
|
|
|
" articleNumber," + |
|
122
|
|
|
" productName," + |
|
123
|
|
|
" productDescription," + |
|
124
|
|
|
" productSpecifiers," + |
|
125
|
|
|
" stock," + |
|
126
|
|
|
" location, " + |
|
127
|
|
|
" price," + |
|
128
|
|
|
"'" + apiKey + "'" + |
|
129
|
|
|
" FROM products" + |
|
130
|
|
|
" WHERE apiKey = ?"; |
|
131
|
|
|
|
|
132
|
|
|
db.run(sql, copyApiKey, (err) => { |
|
133
|
|
|
if (err) { |
|
134
|
|
|
return res.status(500).json({ |
|
135
|
|
|
errors: { |
|
136
|
|
|
status: 500, |
|
137
|
|
|
source: "/copy_products", |
|
138
|
|
|
title: "Database error", |
|
139
|
|
|
detail: err.message |
|
140
|
1 |
|
} |
|
141
|
2 |
|
}); |
|
142
|
|
|
} else { |
|
|
|
|
|
|
143
|
|
|
products.getAllProducts(res, apiKey, 201); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
}); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
function copyOrders(res, apiKey) { |
|
149
|
|
|
let sql = "INSERT INTO orders" + |
|
150
|
|
|
" (orderId," + |
|
151
|
1 |
|
" customerName," + |
|
152
|
|
|
" customerAddress," + |
|
153
|
|
|
" customerZip," + |
|
154
|
|
|
" customerCity," + |
|
155
|
|
|
" customerCountry," + |
|
156
|
|
|
" statusId," + |
|
157
|
1 |
|
" apiKey)" + |
|
158
|
|
|
" SELECT orderId," + |
|
159
|
|
|
" customerName," + |
|
160
|
|
|
" customerAddress," + |
|
161
|
|
|
" customerZip," + |
|
162
|
|
|
" customerCity," + |
|
163
|
|
|
" customerCountry," + |
|
164
|
|
|
" statusId," + |
|
165
|
|
|
"'" + apiKey + "'" + |
|
166
|
|
|
" FROM orders" + |
|
167
|
|
|
" WHERE apiKey = ?"; |
|
168
|
|
|
|
|
169
|
|
|
db.run(sql, copyApiKey, (err) => { |
|
170
|
|
|
if (err) { |
|
171
|
|
|
return res.status(500).json({ |
|
172
|
|
|
errors: { |
|
173
|
|
|
status: 500, |
|
174
|
|
|
source: "/copy_orders", |
|
175
|
|
|
title: "Database error", |
|
176
|
|
|
detail: err.message |
|
177
|
1 |
|
} |
|
178
|
2 |
|
}); |
|
179
|
|
|
} else { |
|
|
|
|
|
|
180
|
|
|
let orderItemsSQL = "INSERT INTO order_items" + |
|
181
|
|
|
" (orderId," + |
|
182
|
|
|
" productId," + |
|
183
|
|
|
" amount," + |
|
184
|
|
|
" apiKey)" + |
|
185
|
|
|
" SELECT orderId," + |
|
186
|
|
|
" productId," + |
|
187
|
|
|
" amount," + |
|
188
|
1 |
|
"'" + apiKey + "'" + |
|
189
|
|
|
" FROM order_items" + |
|
190
|
|
|
" WHERE apiKey = ?"; |
|
191
|
|
|
|
|
192
|
|
|
db.run(orderItemsSQL, copyApiKey, (err) => { |
|
193
|
|
|
if (err) { |
|
194
|
|
|
return res.status(500).json({ |
|
195
|
|
|
errors: { |
|
196
|
|
|
status: 500, |
|
197
|
|
|
source: "/copy_orders", |
|
198
|
|
|
title: "Database error in order_items", |
|
199
|
|
|
detail: err.message |
|
200
|
1 |
|
} |
|
201
|
2 |
|
}); |
|
202
|
|
|
} else { |
|
|
|
|
|
|
203
|
|
|
orders.getAllOrders(res, apiKey, 201); |
|
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
}); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
}); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return { |
|
211
|
1 |
|
copyAll: copyAll, |
|
212
|
|
|
copyProducts: copyProducts, |
|
213
|
|
|
copyOrders: copyOrders, |
|
214
|
|
|
}; |
|
215
|
|
|
}()); |
|
216
|
|
|
|
|
217
|
|
|
module.exports = copier; |
|
218
|
|
|
|